

import 'package:flutter/cupertino.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';

import '../../../../commonTool/fm_report_event_tool.dart';
import '../../../../commonTool/fm_time_tool.dart';
import '../../../../commonUI/fm_progress_hud.dart';
import '../../../../commonUI/fm_toast.dart';
import '../../controller/fm_common_controller.dart';
import '../personal/fm_personal_model.dart';
import 'fm_job_api.dart';
import 'fm_save_job_api.dart';

class FMJobController extends GetxController {

  List<FMPersonalIncidentsModel>? dataSource;

  Map<String,dynamic> uploadDic = {};

  String orderId = "";

  Position? position;

  int startTime = 0;

  @override
  void onInit() {
    super.onInit();
    startTime = FMTimeTool.currentTime;
  }

  void refreshData(String orderId) {
    this.orderId = orderId;
    FMJobApi api = FMJobApi(
        orderId: orderId
    );
    api.send<FMPersonalModel,FMPersonalModel>(decodeType: FMPersonalModel()).then((value){
      FMProgressHud.hideLoading();
      if (value.data != null && value.data!.incidents != null) {
        dataSource = value.data!.incidents;

        for (FMPersonalIncidentsModel model in dataSource!) {
          String? key = model.cheers;
          String? value = getSinglePopValue(model);

          if (key != null && key.isNotEmpty) {
            uploadDic[key] = value;
          }
        }

      }
      update();
    });
  }

  void saveInfo(BuildContext context,bool? isOldPage) {
    Map <String,dynamic> params = {
      "listen" : orderId
    };

    for (var key in uploadDic.keys) {
      params[key] = uploadDic[key];
    }

    FMSaveJobApi api = FMSaveJobApi(params: params);

    FMProgressHud.showLoading();

    api.send().then((value) {
      FMProgressHud.hideLoading();
      if (value.code == 0){
        reportRisk();

        if (isOldPage == true) {
          Navigator.pop(context);
          return;
        }

        final controller = Get.put(FMCommonController());
        controller.onRequestNoEmptyListen(context, orderId);
      }else {
        FMToast.showText(value.message ?? "Network request error");
      }
    });

  }

  bool isNeedClicked(FMPersonalIncidentsModel? model) {
    return model?.sent != "his";
  }

  String contentAtIndex(FMPersonalIncidentsModel? model) {

    String? key = model?.cheers;

    String? value = uploadDic[key];

    if (model?.sent != "could") {
      return value ?? "";
    }else {
      List<FMPersonalRetiredModel>? retired = model?.retired;
      if (retired != null && retired.isNotEmpty) {
        String text = "";
        for (FMPersonalRetiredModel model in retired) {
          if (model.firm.toString() == value) {
            text = model.planned ?? "";
            break;
          }
        }
        return text;
      }else {
        return "";
      }
    }
  }

  void updateDic(FMPersonalIncidentsModel? model,String text) {

    String? key = model?.cheers;

    List<FMPersonalRetiredModel>? retired = model?.retired;

    if (model?.sent != "could") {
      if (key != null && key.isNotEmpty) {
        uploadDic[key] = text;
      }
      if (model?.sent == "wife"){
        update();
      }
    }else {
      if (key != null && key.isNotEmpty) {
        if (retired != null && retired.isNotEmpty) {
          String value = "";
          for (FMPersonalRetiredModel model in retired) {
            if (model.planned == text) {
              value = model.firm!.toString();
              break;
            }
          }
          uploadDic[key] = value;
          update(["updateCell"]);
        }
      }
    }
  }

  TextInputType textInputType(FMPersonalIncidentsModel? model) {
    return model?.dreaming == 1 ? TextInputType.number : TextInputType.text;
  }

  String getSinglePopValue(FMPersonalIncidentsModel model) {
    if (model.sent == "could") {
      List<FMPersonalRetiredModel>? retired = model.retired;

      if (retired == null || retired.isEmpty) return "";
      String value = "";
      for (FMPersonalRetiredModel m in retired) {
        if (m.planned == model.invent) {
          value = m.firm.toString() ?? "";
          break;
        }
      }
      return value;
    }else {
      return model.invent ?? "";
    }
  }

  void reportRisk() {
    int endTime = FMTimeTool.currentTime;
    FMReportEventTool.reportRisk("6", position?.latitude, position?.longitude, startTime, endTime);
  }
}